package cn.edu.neusoft.fragment112gxl.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.fragment112gxl.R;
import cn.edu.neusoft.fragment112gxl.fragment.AddressFragment;
import cn.edu.neusoft.fragment112gxl.fragment.FriendFragment;
import cn.edu.neusoft.fragment112gxl.fragment.SettingFragment;
import cn.edu.neusoft.fragment112gxl.fragment.WeixinFragment;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private LinearLayout layout1, layout2, layout3, layout4;
    private ImageButton button1, button2, button3, button4;
    private List<Fragment> listFragment;
    private FragmentPagerAdapter fragmentPagerAdapter;

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resetImage();
            switch (v.getId())  {
                case R.id.layout1:selectTab(0); break;
                case R.id.layout2:selectTab(1); break;
                case R.id.layout3:selectTab(2); break;
                case R.id.layout4:selectTab(3); break;
            }
        }
    };

    private void selectTab(int i) {
        switch (i){
            case 0:
                button1.setImageResource(R.drawable.weixin_pressed);
                break;
            case 1:
                button2.setImageResource(R.drawable.friend_pressed);
                break;
            case 2:
                button3.setImageResource(R.drawable.address_pressed);
                break;
            case 3:
                button4.setImageResource(R.drawable.settings_pressed);
                break;
        }
        viewPager.setCurrentItem(i);
    }

    private void resetImage() {
        button1.setImageResource(R.drawable.weixin_normal);
        button2.setImageResource(R.drawable.friend_normal);
        button3.setImageResource(R.drawable.address_normal);
        button4.setImageResource(R.drawable.settings_normal);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        layout1.setOnClickListener(onClickListener);
        layout2.setOnClickListener(onClickListener);
        layout3.setOnClickListener(onClickListener);
        layout4.setOnClickListener(onClickListener);
        initFragment();


    }

    private void initFragment() {
        listFragment=new ArrayList<Fragment>();
        listFragment.add(new WeixinFragment());
        listFragment.add(new AddressFragment());
        listFragment.add(new SettingFragment());
        listFragment.add(new FriendFragment());

        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listFragment.get(position);
            }

            @Override
            public int getCount() {
                return listFragment.size();
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewPager.setCurrentItem(position);
                resetImage();
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    private void init() {
        layout1 = (LinearLayout) findViewById(R.id.layout1);
        layout2 = (LinearLayout) findViewById(R.id.layout2);
        layout3 = (LinearLayout) findViewById(R.id.layout3);
        layout4 = (LinearLayout) findViewById(R.id.layout4);

        button1 = (ImageButton) findViewById(R.id.imageButton1);
        button2 = (ImageButton) findViewById(R.id.imageButton2);
        button3 = (ImageButton) findViewById(R.id.imageButton3);
        button4 = (ImageButton) findViewById(R.id.imageButton4);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
    }


}
